<?php

require_once 'BaseController.php';

class Admin_UserController extends BaseController {
	
	//用户列表
	public function userListAction() {
		try{
			$type = $this->request->getParam('type','user');
			$user_infoDAO = $this->orm->createDAO('_users_info')->alias('u');
			$user_groupDAO = $user_infoDAO->with_users_group(array('group_id'=>'group_id'))->alias('g');
			$user_info_app_extDAO = $user_infoDAO ->with_users_info_app_ext(array('user_id'=>'user_id'))->alias('a');
			$user_infoDAO ->select('u.*, g.*, a.user_app_ext1');
			$user_infoDAO->order('u.user_name asc');
			if($type == 'user') {
				$user_groupDAO->findGroup_type(0);
				$user_infoDAO->selectLimit .= ' OR u.group_id = 0 ';
			}elseif($type == 'admin') {
				$user_groupDAO->findGroup_type(1);
			}else {
				throw new Exception('非法输入！',500); 
			}
			$perPage = 15;
			
			$user_infoDAO->getPager(array('path'=>__PROJECTNAME__ . '/admin/user/user-list/type/' . $type,'perPage'=>$perPage,'nextImg'=>'»','prevImg'=>'«'))->assignTo($this->view);
			
			$this->view->assign(array(
				'type'		=> $type,
				'perPage'	=> $perPage
			));
			
			echo $this->view->render('user/user-list.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//添加管理员
	public function addAdminAction() {
		try {
			if($_POST) {
				$username = HttpUtil::postString('username');
				$email = HttpUtil::postString('email');
				//提交时检查用户名,email是否存在
				if(HttpUtil::isJsonRequest()) {
					$userDAO = $this->orm->createDAO('_users_info')->findUser_name($username);
					$userDAO->selectLimit .= " OR email = '$email' ";
					$userDAO = $userDAO->get();
					
					if(!empty($userDAO)) {
						echo json_encode(array('success'=>0,'msg'=>'用户名或者email以注册！'));
					}else {
						echo json_encode(array('success'=>1));
					}
					exit;
				}
				$pwd = HttpUtil::postString('password');
				$con_pwd = HttpUtil::postString('password_confirm');
				$gid = HttpUtil::postString('group_id');
				
				//校验
				$error_msg = '';
				if($username == '') {
					$error_msg .= '请填写用户名！\n';
				}else {
					$user_info = $this->orm->createDAO('_users_info')->findUser_name($username)->get();
					if(!empty($user_info)) {
						echo '<script type="text/javascript">alert("用户名已存在！");</script>';
						exit;
					}
					$temp_val = uc_user_checkname($username);
					switch($temp_val) {
						case -1:$error_msg .= '用户名不合法！\n';break;
						case -2:$error_msg .= '包含要允许注册的词语！\n';break;
					}
				}
				if($pwd != $con_pwd) {
					$error_msg .= '请确认您两次输入的密码一致！\n';
				}elseif($pwd == '' || $con_pwd == '') {
					$error_msg .= '请输入密码！\n';
				}
				if($email == '') {
					$error_msg .= '请输入email！\n';
				}else {
					$temp_val = uc_user_checkemail($email);
					switch($temp_val) {
						case -4:$error_msg .= 'Email 格式有误！\n';break;
						case -5:$error_msg .= 'Email 不允许注册！\n';break;
					}
				}
				if($error_msg != '') {
					echo '<script type="text/javascript">alert("' . $error_msg . '");</script>';
					exit;
				}
				
				//注册用户
				$temp_val = uc_user_register($username,$pwd,$email);
				if($temp_val < 0) {
					switch($temp_val) {
						case -1:$error_msg .= '用户名不合法！\n';break;
						case -2:$error_msg .= '包含要允许注册的词语！\n';break;
						case -3:$error_msg .= '用户名已经存在！\n';break;
						case -4:$error_msg .= 'Email 格式有误！\n';break;
						case -5:$error_msg .= 'Email 不允许注册！\n';break;
						case -6:$error_msg .= '该 Email 已经被注册！\n';break;
					}
					
					if($temp_val == -3) {
						$temp_val = uc_user_login($username,$pwd);
						if($temp_val[0] > 0) {
							if($this->saveToUserInfo($temp_val[0],$username,$pwd,$email,$gid)) {
								echo '<script type="text/javascript">alert("注册成功！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list/type/admin"</script>';
								exit;
							}else {
								echo '<script type="text/javascript">alert("注册失败！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list/type/admin"</script>';
								exit;
							}
						}
					}
					
					echo '<script type="text/javascript">alert("' . $error_msg . '");</script>';
					exit;
				}else {
					if($this->saveToUserInfo($temp_val,$username,$pwd,$email,$gid)) {
						echo '<script type="text/javascript">alert("注册成功！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list/type/admin"</script>';
						exit;
					}else {
						echo '<script type="text/javascript">alert("注册失败！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list/type/admin"</script>';
						exit;
					}
				}
				exit;
			}
			
			$this->view->assign(array(
				'group_info'		=> $this->orm->createDAO('_users_group')->findGroup_type(1)->get()
			));
			
			echo $this->view->render('user/add-admin.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			if(HttpUtil::isJsonRequest()) {
				echo json_encode(array('success'=>0,'msg'=>$e->getMessage()));
				exit;
			}
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//管理员注册写入用户表
	private function saveToUserInfo($uid,$username,$password,$email,$gid) {
		try {
			$userDAO = $this->orm->createDAO('_users_info');
			$userDAO->user_name = $username;
			$userDAO->user_id = $uid;
			$userDAO->password = md5($password);
			$userDAO->email = $email;
			$userDAO->group_id = $gid;
			$userDAO->reg_time = time();
			$userDAO->last_login = time();
			$userDAO->reg_ip = $_SERVER['REMOTE_ADDR'];
			$userDAO->last_ip = $_SERVER['REMOTE_ADDR'];
			
			return $userDAO->save();
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//删除管理员
	public function deleteAdminAction() {
		try {
			$uid = HttpUtil::getString('uid');
			
			$this->orm->createDAO('_users_info')->findUser_id($uid)->delete();
			$this->alert_go('删除成功！',__PROJECTNAME__ . '/admin/user/user-list/type/admin');
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	public function editAdminAction() {
		try {
			$uid = HttpUtil::getString('uid');
			$userDAO = $this->orm->createDAO('_users_info')->findUser_id($uid);
			$user_info = $userDAO->get();
			
			if($_POST) {
				foreach($_POST as $k=>$v) {
					$$k = $v;
				}
				
				$error_msg = '';
				
				if($email == '') {
					$error_msg .= '请输入email！\n';
				}else {
					if($email == $user_info[0]['email']) {
						$email = '';
					}else {
						$temp_val = uc_user_checkemail($email);
						switch($temp_val) {
							case -4:$error_msg .= 'Email 格式有误！\n';break;
							case -5:$error_msg .= 'Email 不允许注册！\n';break;
							case -6:$error_msg .= '该 Email 已经被注册！\n';break;
						}
					}
				}
				if($error_msg != '') {
					echo '<script type="text/javascript">alert("' . $error_msg . '");</script>';
					exit;
				}
				if($password != '') {
					if(md5($password) == $user_info[0]['password']) {
						$password = '';
					}
				}
				if($group_id == $user_info[0]['group_id']) {
					$group_id = '';
				}
				
				$temp_val = uc_user_edit($username,'',$password,$email,1);
				switch($temp_val) {
					case -1:$error_msg .= '旧密码不正确！\n';break;
					case -4:$error_msg .= 'Email 格式有误！\n';break;
					case -5:$error_msg .= 'Email 不允许注册！\n';break;
					case -6:$error_msg .= '该 Email 已经被注册！\n';break;
					case -8:$error_msg .= '该用户受保护无权限更改！\n';break;
				}
				if($error_msg != '') {
					echo '<script type="text/javascript">alert("' . $error_msg . '");</script>';
					exit;
				}
				if($temp_val == 1) {
					$userDAO = $this->orm->createDAO('_users_info')->findUser_id($uid);
					if($email != '') {
						$userDAO->email = $email;
					}
					if($password != '') {
						$userDAO->password = $password;
					}
					if($group_id != '') {
						$userDAO->group_id = $group_id;
					}
					$userDAO->save();
					
					echo '<script type="text/javascript">alert("更新成功！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list/type/admin"</script>';
					exit;
				}else {
					echo '<script type="text/javascript">alert("数据无更新！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list/type/admin"</script>';
					exit;
				}
			}
			
			$this->view->assign(array(
				'user_info'			=> $user_info[0],
				'group_info'		=> $this->orm->createDAO('_users_group')->findGroup_type(1)->get()
			));
			
			echo $this->view->render('user/add-admin.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//添加用户
	public function addUserAction() {
		try {
			if($_POST) {
				$username = HttpUtil::postString('username');
				$email = HttpUtil::postString('email');
				//提交时检查用户名,email是否存在
				if(HttpUtil::isJsonRequest()) {
					$userDAO = $this->orm->createDAO('_users_info')->findUser_name($username);
					$userDAO->selectLimit .= " OR email = '$email' ";
					$userDAO = $userDAO->get();
					
					if(!empty($userDAO)) {
						echo json_encode(array('success'=>0,'msg'=>'用户名或者email以注册！'));
					}else {
						echo json_encode(array('success'=>1));
					}
					exit;
				}
				$pwd = HttpUtil::postString('password');
				$con_pwd = HttpUtil::postString('password_confirm');
				$gid = HttpUtil::postString('group_id');
				
				//校验
				$error_msg = '';
				if($username == '') {
					$error_msg .= '请填写用户名！\n';
				}else {
					$user_info = $this->orm->createDAO('_users_info')->findUser_name($username)->get();
					if(!empty($user_info)) {
						echo '<script type="text/javascript">alert("用户名已存在！");</script>';
						exit;
					}
					$temp_val = uc_user_checkname($username);
					switch($temp_val) {
						case -1:$error_msg .= '用户名不合法！\n';break;
						case -2:$error_msg .= '包含要允许注册的词语！\n';break;
					}
				}
				if($pwd != $con_pwd) {
					$error_msg .= '请确认您两次输入的密码一致！\n';
				}elseif($pwd == '' || $con_pwd == '') {
					$error_msg .= '请输入密码！\n';
				}
				if($email == '') {
					$error_msg .= '请输入email！\n';
				}else {
					$temp_val = uc_user_checkemail($email);
					switch($temp_val) {
						case -4:$error_msg .= 'Email 格式有误！\n';break;
						case -5:$error_msg .= 'Email 不允许注册！\n';break;
					}
				}
				if($error_msg != '') {
					echo '<script type="text/javascript">alert("' . $error_msg . '");</script>';
					exit;
				}
				
				//注册用户
				$temp_val = uc_user_register($username,$pwd,$email);
				if($temp_val < 0) {
					switch($temp_val) {
						case -1:$error_msg .= '用户名不合法！\n';break;
						case -2:$error_msg .= '包含要允许注册的词语！\n';break;
						case -3:$error_msg .= '用户名已经存在！\n';break;
						case -4:$error_msg .= 'Email 格式有误！\n';break;
						case -5:$error_msg .= 'Email 不允许注册！\n';break;
						case -6:$error_msg .= '该 Email 已经被注册！\n';break;
					}
					
					if($temp_val == -3) {
						$temp_val = uc_user_login($username,$pwd);
						if($temp_val[0] > 0) {
							if($this->saveToUserInfo($temp_val[0],$username,$pwd,$email,$gid)) {
								echo '<script type="text/javascript">alert("注册成功！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list"</script>';
								exit;
							}else {
								echo '<script type="text/javascript">alert("注册失败！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list"</script>';
								exit;
							}
						}
					}
					
					echo '<script type="text/javascript">alert("' . $error_msg . '");</script>';
					exit;
				}else {
					if($this->saveToUserInfo($temp_val,$username,$pwd,$email,$gid)) {
						echo '<script type="text/javascript">alert("注册成功！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list"</script>';
						exit;
					}else {
						echo '<script type="text/javascript">alert("注册失败！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list"</script>';
						exit;
					}
				}
				exit;
			}
			
			$this->view->assign(array(
				'group_info'		=> $this->orm->createDAO('_users_group')->findGroup_type(0)->get()
			));
			
			echo $this->view->render('user/add-admin.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			if(HttpUtil::isJsonRequest()) {
				echo json_encode(array('success'=>0,'msg'=>$e->getMessage()));
				exit;
			}
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//编辑用户
	public function editUserAction() {
		try {
			$uid = HttpUtil::getString('uid');
			$userDAO = $this->orm->createDAO('_users_info')->findUser_id($uid);
			$user_info = $userDAO->get();
			
			if($_POST) {
				foreach($_POST as $k=>$v) {
					$$k = $v;
				}
				
				$error_msg = '';
				
				if($email == '') {
					$error_msg .= '请输入email！\n';
				}else {
					if($email == $user_info[0]['email']) {
						$email = '';
					}else {
						$temp_val = uc_user_checkemail($email);
						switch($temp_val) {
							case -4:$error_msg .= 'Email 格式有误！\n';break;
							case -5:$error_msg .= 'Email 不允许注册！\n';break;
							case -6:$error_msg .= '该 Email 已经被注册！\n';break;
						}
					}
				}
				if($error_msg != '') {
					echo '<script type="text/javascript">alert("' . $error_msg . '");</script>';
					exit;
				}
				if($password != '') {
					if(md5($password) == $user_info[0]['password']) {
						$password = '';
					}
				}
				if($group_id == $user_info[0]['group_id']) {
					$group_id = '';
				}
				
				$temp_val = uc_user_edit($username,'',$password,$email,1);
				switch($temp_val) {
					case -1:$error_msg .= '旧密码不正确！\n';break;
					case -4:$error_msg .= 'Email 格式有误！\n';break;
					case -5:$error_msg .= 'Email 不允许注册！\n';break;
					case -6:$error_msg .= '该 Email 已经被注册！\n';break;
					case -8:$error_msg .= '该用户受保护无权限更改！\n';break;
				}
				if($error_msg != '') {
					echo '<script type="text/javascript">alert("' . $error_msg . '");</script>';
					exit;
				}
				if($temp_val == 1) {
					$userDAO = $this->orm->createDAO('_users_info')->findUser_id($uid);
					if($email != '') {
						$userDAO->email = $email;
					}
					if($password != '') {
						$userDAO->password = $password;
					}
					if($group_id != '') {
						$userDAO->group_id = $group_id;
					}
					$userDAO->save();
					
					echo '<script type="text/javascript">alert("更新成功！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list"</script>';
					exit;
				}else {
					echo '<script type="text/javascript">alert("数据无更新！");parent.location.href="' . __PROJECTNAME__ . '/admin/user/user-list"</script>';
					exit;
				}
			}
			
			$this->view->assign(array(
				'user_info'			=> $user_info[0],
				'group_info'		=> $this->orm->createDAO('_users_group')->findGroup_type(0)->get()
			));
			
			echo $this->view->render('user/add-admin.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//删除用户
	public function deleteUserAction() {
		try {
			$uid = HttpUtil::getString('uid');
			
			//if(uc_user_delete($uid)) {
				$this->orm->createDAO('_users_info')->findUser_id($uid)->delete();
				$this->alert_go('删除完成！','/admin/user/user-list');
			//}
			
			$this->alert_back('删除失败！');
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
}